(function(){
  'use strict';
/**
 * @ngdoc RMqueryRoleModule
 * @name cmmdApp  
 * @systemManger 聂开枥
 */
angular.module('controllers.faultLocation.alarmStatisticAnalysis')
  .controller('compareAnalysisCtrl', ['$scope', 'endAnalysisServer', '$mdDialog', 'CoreService', compareAnalysis]);
/**
 *  下拉选项
 *  给默认初始值时间为当前时间 
 *  省份,业务名称,系统名称,显示方式为请选择字样,【组建需修改,前台无法与控制器双向绑定】
 */
function compareAnalysis($scope, endAnalysisServer, $mdDialog, CoreService) {
  /**
   * 获取利用流水号查询业务时间分析查询参赛
   * 给页面可读框复制
   */

  $scope.ETE = endAnalysisServer.getMessage();
  var demo = echarts.init(document.getElementById('main'));
  /**
   * 查询到流水号业务的时长分析值:包括平均时长avgSeriesData,真实时长:realSeriesData
   * 
   *
   *  例如：for(var key in result.data.avg){
   *        xAxisData.push(key);
   *        avgSeriesData.push(result.data.avg[key]);
   *    };
   *
   * 定义的数组,迭代jsin数据得到key,和value的值.分别push到数组中
   *
   * 得到数据,调用图例方法:
   *   发送请求查询 查询真实时间和平均时间图例
   */

  /**
   * 初始化方法  进入页面就有图例
   */
  var xAxisData = [];
  var avgSeriesData = [];
  var realSeriesData = [];
  $scope.shoumun = 1;

  $scope.dropDrows = function() {
    if ($scope.tradeForm.$invalid) {
      return;
    }
    if (Boolean($scope.shoumun)) {
      xAxisData = [];
      avgSeriesData = [];
      realSeriesData = [];
      $scope.isResponded = true;
      graph();
    } else {
      $scope.shoumun = 1;
    }
  };


  function graph() {
    if ($scope.ETE.serialNum.length > 32) {
      var str = '长度过程';
      $scope.isResponded = false;
      CoreService.dialog(str, '警告');
      $scope.shoumun = 1;
    } else {
      $scope.isResponded = false;
      endAnalysisServer.compare($scope.ETE, function(result) {
        if (result.state !== '10000') {
          CoreService.dialog(result.state, '错误代码');
          $scope.shoumun = 1;
        } else {
          $scope.shoumun = 0;
          for (var key in result.data.avg) {
            if (result.data.avg[key] !== null) {
              xAxisData.push(key);
              avgSeriesData.push(result.data.avg[key]);
            }
          }
          for (key in result.data.real) {
            if (result.data.real[key] !== null) {
              realSeriesData.push(result.data.real[key].toFixed(2));
            }
          }
          var textName = $scope.ETE.businessName + '端到端细分环节平均值';
          var subtext = $scope.ETE.date + '--' + $scope.ETE.provinceName;
          legend(subtext, textName, xAxisData, avgSeriesData, realSeriesData);
        }
      });
    }
  }
  /**
   *  初始化查询方法
   */
  $scope.init = function() {
    xAxisData = [];
    avgSeriesData = [];
    realSeriesData = [];
    graph();
  };


  var dataStyle = {
    normal: {
      label: {
        show: true,
        position: 'top'
      }
    }
  };
  /**
   * 
   * 图例方法:给的值是4个图形名字，x轴数据，平均时长，真实时长
   */
  function legend(subtext, textName, xAxisData, avgSeriesData, realSeriesData) {
    var option = {
      title: {
        text: textName,
        x: 'center',
        y: '20',
        subtext: subtext
      },
      tooltip: {
        trigger: 'axis'
      },
      xAxis: [{
        type: 'category',
        data: xAxisData,
        name: '各环节名称'
      }],
      yAxis: [{
        type: 'value',
        name: '处理时长(/s)'
      }],
      legend: {
        data: ['真实值', '平均值'],
        orient: 'vertical',
        x: '95%',
        y: '50%'
      },
      series: [{
        type: 'bar',
        data: realSeriesData,
        itemStyle: dataStyle,
        name: '真实值'
      }, {
        type: 'bar',
        data: avgSeriesData,
        itemStyle: dataStyle,
        name: '平均值'
      }]
    };
    demo.setOption(option);
  }

}
})();